<template>
  <div class="container">
    <h2>跨列表拖拽示例</h2>
    <div class="lists-container">
      <!-- 第一个列表 -->
      <div class="list-container">
        <h3>待办事项</h3>
        <draggable v-model="todoList" group="tasks" class="drag-list" @end="onDragEnd">
          <div v-for="item in todoList" :key="item.id" class="list-item">
            <span class="handle">☰</span>
            {{ item.name }}
            <span class="status-badge">待办</span>
          </div>
        </draggable>
      </div>

      <!-- 第二个列表 -->
      <div class="list-container">
        <h3>已完成</h3>
        <draggable v-model="doneList" group="tasks" class="drag-list" @end="onDragEnd">
          <div v-for="item in doneList" :key="item.id" class="list-item done">
            <span class="handle">☰</span>
            {{ item.name }}
            <span class="status-badge">完成</span>
          </div>
        </draggable>
      </div>
    </div>

    <!-- 数据展示 -->
    <div class="data-view">
      <h3>当前数据状态</h3>
      <div class="data-columns">
        <div>
          <h4>待办事项:</h4>
          <pre>{{ JSON.stringify(todoList, null, 2) }}</pre>
        </div>
        <div>
          <h4>已完成:</h4>
          <pre>{{ JSON.stringify(doneList, null, 2) }}</pre>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import draggable from 'vuedraggable'

export default {
  components: {
    draggable
  },
  data() {
    return {
      todoList: [
        { id: 1, name: '学习Vue', status: 'todo' },
        { id: 2, name: '编写文档', status: 'todo' },
        { id: 3, name: '项目会议', status: 'todo' }
      ],
      doneList: [
        { id: 4, name: '购买食材', status: 'done' },
        { id: 5, name: '晨跑锻炼', status: 'done' }
      ]
    }
  },
  methods: {
    onDragEnd(evt) {
      console.log('evt',evt)
      // 更新状态
      // if (evt.to.className.includes('drag-list')) {
      //   const listName = evt.to.parentElement.querySelector('h3').textContent
      //   const newStatus = listName === '待办事项' ? 'todo' : 'done'
      //
      //   const movedItem = this[listName === '待办事项' ? 'todoList' : 'doneList'][evt.newIndex]
      //   if (movedItem) {
      //     movedItem.status = newStatus
      //   }
      // }

      console.log('拖拽完成，当前数据:', {
        todoList: this.todoList,
        doneList: this.doneList
      })
    }
  }
}
</script>

<style>
.container {
  max-width: 1000px;
  margin: 0 auto;
  padding: 20px;
  font-family: Arial, sans-serif;
}

h2 {
  text-align: center;
  color: #333;
}

.lists-container {
  display: flex;
  justify-content: space-between;
  margin: 30px 0;
  gap: 20px;
}

.list-container {
  flex: 1;
  background: #f5f7fa;
  padding: 15px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.list-container h3 {
  margin-top: 0;
  color: #409EFF;
  padding-bottom: 10px;
  border-bottom: 1px solid #ddd;
}

.drag-list {
  min-height: 200px;
  padding: 10px;
  background: white;
  border-radius: 4px;
  border: 1px dashed #ddd;
}

.list-item {
  padding: 12px 15px;
  margin-bottom: 8px;
  background: #fff;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  display: flex;
  align-items: center;
  cursor: move;
  transition: all 0.3s;
}

.list-item:hover {
  box-shadow: 0 2px 6px 0 rgba(0, 0, 0, 0.1);
}

.list-item.done {
  background: #f0f9eb;
  border-color: #e1f3d8;
}

.handle {
  margin-right: 10px;
  cursor: move;
  color: #999;
}

.status-badge {
  margin-left: auto;
  padding: 2px 8px;
  font-size: 12px;
  border-radius: 10px;
  background: #409EFF;
  color: white;
}

.list-item.done .status-badge {
  background: #67C23A;
}

.data-view {
  margin-top: 40px;
  background: #f5f7fa;
  padding: 20px;
  border-radius: 8px;
}

.data-columns {
  display: flex;
  gap: 20px;
}

.data-columns > div {
  flex: 1;
  background: white;
  padding: 15px;
  border-radius: 4px;
}

pre {
  background: #f9f9f9;
  padding: 10px;
  border-radius: 4px;
  overflow-x: auto;
}
</style>